package com.changgou.goods.controller;

import com.changgou.goods.pojo.Album;
import com.changgou.goods.service.AlbumService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/album")
@CrossOrigin
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    /****
     * 查找所有相册列表
     * @return
     */
    @GetMapping
    public Result<List<Album>> findAll() {
        List<Album> albums = albumService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", albums);
    }

    /****
     * 添加相册
     * @param album
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Album album) {
        albumService.add(album);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    /****
     * 条件查询所有
     * @param album
     * @return
     */
    @PostMapping("/search")
    public Result<List<Album>> findList(@RequestBody(required = false) Album album) {
        List<Album> albums = albumService.findList(album);
        return new Result<>(true, StatusCode.OK, "查询成功", albums);
    }

    /****
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<Album> pageInfo = albumService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /****
     * 分页条件查询
     * @param page
     * @param size
     * @param album
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable Integer page, @PathVariable Integer size,
                                     @RequestBody(required = false) Album album) {
        PageInfo<Album> pageInfo = albumService.findPage(page, size, album);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /****
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Album> findById(@PathVariable Long id) {
        Album album = albumService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", album);
    }

    /****
     * 根据Id修改
     * @param id
     * @param album
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable Long id, @RequestBody Album album) {
        album.setId(id);
        albumService.update(album);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    /****
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        albumService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }
}
